//
//  NKBabyController.m
//  NiKi
//
//  Created by qiang chen on 16/6/23.
//  Copyright © 2016年 qiang chen. All rights reserved.
//

#import "NKBabyController.h"
#import "NKBabyCell.h"
#import "NKBabyReusableView.h"
#import "NKBabyChangeHeadImageView.h"
#import "NKBabyInfoController.h"

#define topInset 280.f/667 * ScreenH
@interface NKBabyController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,NKBabyChangeHeadImageViewDelegete,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    
    __weak IBOutlet UIImageView *_sexImageV;
    __weak IBOutlet UIButton *_nameBtn;
    __weak IBOutlet UIImageView *_headImageV;
    IBOutlet UIView *_headV;
    __weak IBOutlet UICollectionView *_collectionV;
    UIImageView *_collectionBg;
}
@property (nonatomic, strong) NKBabyChangeHeadImageView *choseHeadV;
@property (nonatomic, strong) UIImagePickerController *imagePicker;
@end

@implementation NKBabyController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)setupUI {
    [_collectionV registerNib:[UINib nibWithNibName:NSStringFromClass([NKBabyCell class]) bundle:nil] forCellWithReuseIdentifier:babyCell];
    [_collectionV registerNib:[UINib nibWithNibName:NSStringFromClass([NKBabyReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:babyReusableView];
    _collectionV.contentInset = UIEdgeInsetsMake(topInset, 0, 0, 0);
    _headV.frame = CGRectMake(0, -topInset, ScreenW, topInset);
    [_collectionV addSubview:_headV];
    
    self.view.layer.contents = (id)[UIImage imageNamed:@"背景底图"].CGImage;
    
    _collectionBg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, _collectionV.height)];
    _collectionBg.image = [UIImage imageNamed:@"baby_collectionBg"];
    _collectionBg.alpha = 0.3;
    
    _headImageV.layer.borderWidth = 4.0f;
    _headImageV.layer.borderColor = MainColor.CGColor;
    _headImageV.layer.cornerRadius = topInset*2/3*0.5;
    _headImageV.userInteractionEnabled = YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [_collectionV insertSubview:_collectionBg atIndex:0];
}

- (NKBabyChangeHeadImageView *)choseHeadV {
    if (!_choseHeadV) {
        _choseHeadV = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([NKBabyChangeHeadImageView class]) owner:nil options:nil].firstObject;
        _choseHeadV.delegate = self;
    }
    return _choseHeadV;
}

#pragma mark - 选择宝宝头像
- (IBAction)babyHeadImageClick:(UITapGestureRecognizer *)sender {
    self.choseHeadV.frame = CGRectMake(0, 0, self.view.width, self.view.height);
    [self.view addSubview:self.choseHeadV];
    self.choseHeadV.choseBg.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView animateWithDuration:0.25 animations:^{
        self.choseHeadV.choseBg.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - 登陆
- (IBAction)LoginBtnClick:(UIButton *)sender {
}

#pragma mark - 取名字
- (IBAction)nameBtnClick:(UIButton *)sender {
    [self.navigationController pushViewController:[NKBabyInfoController new] animated:YES];
}

#pragma mark - collectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((ScreenW - 20 - 30)/3.f,(ScreenW - 20 - 30)/3.f);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NKBabyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:babyCell forIndexPath:indexPath];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(ScreenW, 44);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        return [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:babyReusableView forIndexPath:indexPath];
    }
    return nil;
}

#pragma mark - NKBabyChangeHeadImageViewDelegete
- (UIImagePickerController *)imagePicker {
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}

- (void)babyChangeHeadImageView:(NKBabyChangeHeadImageView *)view phoBtnClick:(UIButton *)btn {
    
    self.imagePicker.delegate = self;
    self.imagePicker.sourceType = btn.tag == 30 ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
    self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    
    if (btn.tag == 30) {//拍照
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {//相机可用
            [self presentViewController:self.imagePicker animated:YES completion:nil];
        }else{//相机不可用
           UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"相机不可用,请到设置-通用中设置权限" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }
    }else{//照片
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [self presentViewController:self.imagePicker animated:YES completion:nil];
        }else{
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"图库不可用,请到设置-通用中设置权限" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
    if (originalImage) {
        _headImageV.image = originalImage;
    }
    [self.imagePicker dismissViewControllerAnimated:YES completion:nil];
}
@end
